<template>
  <div class="outForm">
    <el-form :model="form" label-width="120px">
      <el-form-item label="店铺名">
        <el-upload class="avatar-uploader" :show-file-list="false">
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="店铺名">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="店铺评分">
        <el-input v-model="form.score" />
      </el-form-item>
      <el-form-item label="店铺评语">
        <el-input v-model="form.selectedComment" />
      </el-form-item>
      <el-form-item label="店铺地址">
        <el-input v-model="form.address" />
      </el-form-item>
      <el-form-item label="店铺背景图">
        <!-- <el-input v-model="form.name" /> -->
      </el-form-item>

      <!-- 提交内容 -->
      <el-form-item>
        <el-button type="primary" @click="onSubmit">Create</el-button>
        <el-button>Cancel</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from "vue";
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";

import type { UploadProps } from "element-plus";
// do not use same name with ref
const form = reactive({
  // 店铺数据  店铺月销为后端默认给定数值
  name: "",
  score: "",
  selectedComment: "",
  address: "",
});

const imageUrl = ref("");

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!);
};

const beforeAvatarUpload: UploadProps["beforeUpload"] = (rawFile) => {
  if (rawFile.type !== "image/jpeg") {
    ElMessage.error("Avatar picture must be JPG format!");
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("Avatar picture size can not exceed 2MB!");
    return false;
  }
  return true;
};
const onSubmit = () => {
  //打印form表单内容
  console.log(form);
  console.log(imageUrl);
};
</script>

<style lang="scss" scoped>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
.outForm {
  margin: 20px 0;
  padding: 20px;
  width: 100%;
  height: 100%;
  background-color: #fff;
  border-radius: 10px;
  > .el-form {
    width: 500px;
    height: 100%;
  }
}
// 上传图片
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>